package com.xuexiang.xuidemo.app.activity;

import androidx.appcompat.app.AppCompatActivity;
import androidx.viewpager.widget.ViewPager;

import android.content.Intent;
import android.os.Bundle;

import com.xuexiang.xui.adapter.FragmentAdapter;
import com.xuexiang.xui.widget.tabbar.EasyIndicator;
import com.xuexiang.xuidemo.R;
import com.xuexiang.xuidemo.app.DataProvider;
import com.xuexiang.xuidemo.app.adapter.WordListItem;
import com.xuexiang.xuidemo.app.fragment.TestingGif;
import com.xuexiang.xuidemo.app.fragment.TestingImg;
import com.xuexiang.xuidemo.app.fragment.TestingVideo;
import com.xuexiang.xuidemo.base.BaseFragment;
import com.xuexiang.xuidemo.utils.XToastUtils;

import java.util.ArrayList;
import java.util.List;

public class TestingPage extends AppCompatActivity {

    private WordListItem mItem;

    EasyIndicator mEasyIndicator;
    ViewPager mViewPager;

    private String[] tabNames = new String[]{"图片", "动图解析", "真人演示"};

    private List<BaseFragment> list;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.app_activity_testing_page);

        initWordItem();

        mEasyIndicator = findViewById(R.id.easy_indicator);
        mViewPager = findViewById(R.id.view_pager);

        list = new ArrayList<BaseFragment>(){{
            add(TestingImg.newInstance(mItem.image));
            add(TestingGif.newInstance(mItem.gifDisplay));
            add(TestingVideo.newInstance(mItem.video));
        }};

        mEasyIndicator.setTabTitles(tabNames);
        mEasyIndicator.setViewPager(mViewPager, new FragmentAdapter<>(getSupportFragmentManager(), list));

        mViewPager.setOffscreenPageLimit(list.size() - 1);
        mViewPager.addOnPageChangeListener(new ViewPager.OnPageChangeListener() {

            @Override
            public void onPageScrolled(int position, float positionOffset, int positionOffsetPixels) {
            }

            @Override
            public void onPageSelected(int position) {
                onTabChange(position);
            }

            @Override
            public void onPageScrollStateChanged(int state) {
            }
        });
        mEasyIndicator.setOnTabClickListener((title, position) -> onTabChange(position));
    }

    private void onTabChange(int index) {
        XToastUtils.toast(""+index);
    }

    private void initWordItem() {
        Intent intent = getIntent();
        String word = intent.getStringExtra("word");
        mItem = DataProvider.getWordItemByWord(word);
    }

}